package com.ruoyi.web.controller.teacher;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.student.domain.StuZxj;
import com.ruoyi.student.service.IStuZxjService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.teacher.domain.StuZxjInfo;
import com.ruoyi.teacher.service.IStuZxjInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api("教师审批接口")
@RestController
@RequestMapping("/teacher/zxj")
public class StuZxjInfoController extends BaseController {
    @Autowired
    private IStuZxjInfoService stuZxjInfoService;

    @Autowired
    private IStuZxjService stuZxjService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 根据助学金序号查审批记录
     */
    @GetMapping("/{zxjId}")
    public AjaxResult getByZxjId(@PathVariable("zxjId") String zxjId) {
        return AjaxResult.success(stuZxjInfoService.getByZxjId(Long.valueOf(zxjId)));
    }

    /**
     * 根据审批教师id查询该教师需要审批的记录集合
     */
    @GetMapping("/select/{userId}")
    public AjaxResult getByUserId(@PathVariable("userId") String userId) {
        return AjaxResult.success(stuZxjInfoService.getByUserId(Long.valueOf(userId)));
    }

    /**
     * 提交审批
     *
     * @param stuZxjInfo
     * @return AjaxResult
     */
    @ApiOperation("提交审批")
    @Log(title = "教师审批", businessType = BusinessType.INSERT)
    @PostMapping
    @PreAuthorize("@ss.hasPermi('teacher:zxj:shengpi')")
    public AjaxResult saveStuZxjInfo(@RequestBody StuZxjInfo stuZxjInfo) {
        //判断zxjId当前教师有无权限审批
        if (stuZxjInfoService.nengshengpi(stuZxjInfo.getZxjId(), getUserId())) {
            stuZxjInfo.setUserId(getUserId());
            stuZxjInfo.setUpdateBy(sysUserService.selectUserById(getUserId()).getNickName());
            //判断当前是新增的审批还是更新审批
            if (stuZxjInfoService.isNewShengpi(stuZxjInfo.getZxjId())) {
                //ture执行更新
                return AjaxResult.success("更新成功", stuZxjInfoService.updateStuZxjInfo(stuZxjInfo));
            } else {
                //执行新增
                return AjaxResult.success("审批成功", stuZxjInfoService.saveStuZxjInfo(stuZxjInfo));
            }
        } else if (getUserId() == 1) {
            return AjaxResult.error("无法审批。请使用教师账号进行审批。");
        } else {
            return AjaxResult.error("无法审批。当前学生表单不属于自己所管辖的班级范围，请联系管理员授权。");
        }
    }

    /**
     * 更新审批记录
     */
    @PutMapping
    public AjaxResult updateZxjInfo(@RequestBody StuZxjInfo stuZxjInfo) {
        stuZxjInfo.setUpdateTime(DateUtils.getNowDate());
        stuZxjInfo.setUserId(getUserId());
        return toAjax(stuZxjInfoService.updateStuZxjInfo(stuZxjInfo));
    }

    /**
     * 批量删除审批记录
     */
    @DeleteMapping
    public AjaxResult delectByZxjIds(@RequestParam("zxjIds") Long[] zxjIds) {
        return toAjax(stuZxjInfoService.deleteByZxjIds(zxjIds));
    }

    /**
     * 获取审批记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestBody StuZxjInfo stuZxjInfo) {
        startPage();
        List<StuZxjInfo> list = stuZxjInfoService.getStuZxjInfoList(stuZxjInfo);
        return getDataTable(list);
    }

    /**
     * @param stuZxj
     * @return 需审批的助学金信息表(过滤)
     */
    @ApiOperation("需审批的助学金信息表")
    @GetMapping("/listZxj")
    @PreAuthorize("@ss.hasPermi('teacher:zxj:list')")
    public TableDataInfo list(StuZxj stuZxj) {
        startPage();
        List<StuZxj> list = stuZxjService.selectStuZxjListByTeacher(stuZxj);
        return getDataTable(list);
    }

    /**
     * 获取助学金详细信息（审批用）
     */
    @ApiOperation("id查询助学金（教师接口）")
    @PreAuthorize("@ss.hasPermi('teacher:zxj:query')")
    @GetMapping(value = "/query/{zxjId}")
    public AjaxResult query(@PathVariable("zxjId") Long zxjId) {
        //判断zxjId当前教师有无权限审批
        if (stuZxjInfoService.nengshengpi(zxjId, getUserId()) || getUserId() == 1) {
            StuZxj stuZxj = new StuZxj();
            stuZxj.setZxjId(zxjId);
            stuZxj = stuZxjService.queryStuZxj(stuZxj);
            AjaxResult ajaxResult = new AjaxResult();
            //不为空时构造img数组
            if (!com.ruoyi.common.utils.StringUtils.isEmpty(stuZxj.getImageContent())) {
                ArrayList<String> srcList = new ArrayList<>();
                String[] src = stuZxj.getImageContent().split(",@\n");
                for (String src1 : src) {
                    srcList.add(src1);
                }
                stuZxj.setImageContent(null);
                ajaxResult.put("srcList", srcList);
            }
            if (stuZxj.getStatus() != "0" && stuZxj.getStatus() != null) {
                ajaxResult.put("shengpi", stuZxjInfoService.getByZxjId(zxjId));
            }
            ajaxResult.put("data", stuZxj);
            return ajaxResult;
        } else {
            return AjaxResult.error("无法审批。当前学生表单不属于自己所管辖的班级范围，请联系管理员授权。");
        }
    }


}
